package rpc.bootstrap.spring;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import rpc.core.InitClose;
import rpc.utils.ReflectionUtils;

/*********
 *
 *
 *@author 197
 *@date 2020/6/20 23:41
 *
 **/


public class ServiceBean implements InitClose {

  private String interfaceName;
  private String group;
  private int port;
  private int checkPort;
  private Class<?> interfaceClass;
  private Map<String, Method> methodMap = new HashMap<>();


  public int getCheckPort() {
    return checkPort;
  }

  public ServiceBean setCheckPort(int checkPort) {
    this.checkPort = checkPort;
    return this;
  }

  public String getInterfaceName() {
    return interfaceName;
  }

  public ServiceBean setInterfaceName(String interfaceName) {
    this.interfaceName = interfaceName;
    this.interfaceClass = ReflectionUtils.getClass(interfaceName);
    return this;
  }

  public String getGroup() {
    return group;
  }

  public void setGroup(String group) {
    this.group = group;
  }

  public int getPort() {
    return port;
  }

  public void setPort(int port) {
    this.port = port;
  }

  public Map<String, Method> getMethodMap() {
    return methodMap;
  }

  public ServiceBean setMethodMap(Map<String, Method> methodMap) {
    this.methodMap = methodMap;
    return this;
  }


  @Override
  public void init() {
    Class<?> aClass = this.interfaceClass;
    Method[] methods = aClass.getDeclaredMethods();
    Arrays.stream(methods).forEach(method -> {
      int msgId = ReflectionUtils.getFliedIntValue(aClass, method.getName() + "_MsgId");
      methodMap.put(String.valueOf(msgId), method);
    });
  }

  @Override
  public void start() {

  }

  @Override
  public void close() {

  }

  public Class<?> getInterfaceClass() {
    return this.interfaceClass;
  }
}
